<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'srm/cw:prekp:puGeneralPreData:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.pkCorp }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsSrmCwPrekpPuGeneralPreDataList">
  import { unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { puGeneralPreDataDelete, puGeneralPreDataListData } from '/@/api/srm/cw/prekp/puGeneralPreData';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';

  const { t } = useI18n('srm/cw.prekp.puGeneralPreData');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('开票准备区数据管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('公司主键'),
        field: 'pkCorp',
        component: 'Input',
      },
      {
        label: t('source_id'),
        field: 'sourceId',
        component: 'Input',
      },
      {
        label: t('供应商ID'),
        field: 'venId',
        component: 'Input',
      },
      {
        label: t('供应商名称'),
        field: 'venName',
        component: 'Input',
      },
      {
        label: t('采购部门ID'),
        field: 'cgDeptId',
        component: 'Input',
      },
      {
        label: t('采购部门名称'),
        field: 'cgDeptName',
        component: 'Input',
      },
      {
        label: t('业务流程ID'),
        field: 'busType',
        component: 'Input',
      },
      {
        label: t('业务流程名称'),
        field: 'busTypeName',
        component: 'Input',
      },
      {
        label: t('入库单号'),
        field: 'rkCode',
        component: 'Input',
      },
      {
        label: t('入库日期'),
        field: 'rkDate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('订单号'),
        field: 'orderCode',
        component: 'Input',
      },
      {
        label: t('订单日期'),
        field: 'orderDate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('存货ID'),
        field: 'invId',
        component: 'Input',
      },
      {
        label: t('本次开票数'),
        field: 'kpQty',
        component: 'Input',
      },
      {
        label: t('无税单价'),
        field: 'nprice',
        component: 'Input',
      },
      {
        label: t('无税金额'),
        field: 'nmny',
        component: 'Input',
      },
      {
        label: t('含税单价'),
        field: 'ntaxprice',
        component: 'Input',
      },
      {
        label: t('含税金额'),
        field: 'ntaxmny',
        component: 'Input',
      },
      {
        label: t('税率'),
        field: 'nictaxrate',
        component: 'Input',
      },
      {
        label: t('税额'),
        field: 'noritaxmny',
        component: 'Input',
      },
      {
        label: t('准备人名称'),
        field: 'createByName',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('公司主键'),
      dataIndex: 'pkCorp',
      key: 'a.pk_corp',
      sorter: true,
      width: 230,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('source_id'),
      dataIndex: 'sourceId',
      key: 'a.source_id',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('供应商ID'),
      dataIndex: 'venId',
      key: 'a.ven_id',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('供应商名称'),
      dataIndex: 'venName',
      key: 'a.ven_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('采购部门ID'),
      dataIndex: 'cgDeptId',
      key: 'a.cg_dept_id',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('采购部门名称'),
      dataIndex: 'cgDeptName',
      key: 'a.cg_dept_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('业务流程ID'),
      dataIndex: 'busType',
      key: 'a.bus_type',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('业务流程名称'),
      dataIndex: 'busTypeName',
      key: 'a.bus_type_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('入库单号'),
      dataIndex: 'rkCode',
      key: 'a.rk_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('入库日期'),
      dataIndex: 'rkDate',
      key: 'a.rk_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('订单号'),
      dataIndex: 'orderCode',
      key: 'a.order_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('订单日期'),
      dataIndex: 'orderDate',
      key: 'a.order_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('存货ID'),
      dataIndex: 'invId',
      key: 'a.inv_id',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('本次开票数'),
      dataIndex: 'kpQty',
      key: 'a.kp_qty',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('无税单价'),
      dataIndex: 'nprice',
      key: 'a.nprice',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('无税金额'),
      dataIndex: 'nmny',
      key: 'a.nmny',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('含税单价'),
      dataIndex: 'ntaxprice',
      key: 'a.ntaxprice',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('含税金额'),
      dataIndex: 'ntaxmny',
      key: 'a.ntaxmny',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('税率'),
      dataIndex: 'nictaxrate',
      key: 'a.nictaxrate',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('税额'),
      dataIndex: 'noritaxmny',
      key: 'a.noritaxmny',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('准备人名称'),
      dataIndex: 'createByName',
      key: 'a.create_by_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑开票准备区数据'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'srm/cw:prekp:puGeneralPreData:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload }] = useTable({
    api: puGeneralPreDataListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const params = { id: record.id };
    const res = await puGeneralPreDataDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }
</script>
